// FclipDlg.cpp : implementation file
//

#include "stdafx.h"
#include "spritetool.h"
#include "FclipDlg.h"
#include "SpriteDoc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CFclipDlg dialog


CFclipDlg::CFclipDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CFclipDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFclipDlg)
	m_fAngle = 0.0f;
	m_nXRep = 0;
	m_nYRep = 0;
	m_bLock = FALSE;
	m_nPosX = 0;
	m_nPosY = 0;
	m_nOffX = 0;
	m_nOffY = 0;
	//}}AFX_DATA_INIT
	m_pFclip = NULL;
	m_bkfAngle = m_fAngle;
	m_bknXRep = m_nXRep;
	m_bknYRep = m_nYRep;
}


void CFclipDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFclipDlg)
	DDX_Text(pDX, IDC_EDIT_ANGLE, m_fAngle);
	DDX_Text(pDX, IDC_EDIT_XREP, m_nXRep);
	DDX_Text(pDX, IDC_EDIT_YREP, m_nYRep);
	DDX_Check(pDX, IDC_CHECK_LOCK, m_bLock);
	DDX_Text(pDX, IDC_EDIT_POS_X, m_nPosX);
	DDX_Text(pDX, IDC_EDIT_POS_Y, m_nPosY);
	DDX_Text(pDX, IDC_EDIT_XOFF, m_nOffX);
	DDX_Text(pDX, IDC_EDIT_YOFF, m_nOffY);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CFclipDlg, CDialog)
	//{{AFX_MSG_MAP(CFclipDlg)
	ON_WM_PAINT()
	ON_EN_CHANGE(IDC_EDIT_XREP, UpdateFclip)
	ON_EN_CHANGE(IDC_EDIT_YREP, UpdateFclip)
	ON_EN_CHANGE(IDC_EDIT_XOFF, UpdateFclip)
	ON_EN_CHANGE(IDC_EDIT_YOFF, UpdateFclip)
	ON_EN_CHANGE(IDC_EDIT_ANGLE, UpdateFclip)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFclipDlg message handlers

BOOL CFclipDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	if(m_pFclip)
	{
		m_bkfAngle = m_fAngle = m_pFclip->fAngle;
		m_bknXRep = m_nXRep = m_pFclip->nXRep;
		m_bknYRep = m_nYRep = m_pFclip->nYRep;
		m_bknOffX = m_nOffX = m_pFclip->nOffX;
		m_bknOffY = m_nOffY = m_pFclip->nOffY;

		m_nPosX = m_pFclip->nPosX;
		m_nPosY = m_pFclip->nPosY;
		m_bLock = m_pFclip->bLock;
	}
	UpdateData(FALSE);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CFclipDlg::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	if(m_pFclip)
	{
		CWnd* pWnd = GetDlgItem(IDC_STATIC_CLIP);
		if(pWnd != NULL)
		{
			CRect rect;
			pWnd->GetWindowRect(&rect);
			ScreenToClient(&rect);
			CRgn rgnClip;
			if(rgnClip.CreateRectRgn(rect.left, rect.top, rect.right, rect.bottom))
			{
				dc.SelectClipRgn(&rgnClip);

				int x = rect.left - m_pFclip->nPosX;
				int y = rect.top - m_pFclip->nPosY;

				m_pFclip->OnDraw(&dc, x, y, 0);

				dc.SelectClipRgn(NULL);
				rgnClip.DeleteObject();
			}
		}
	}
	// Do not call CDialog::OnPaint() for painting messages
}

void CFclipDlg::UpdateFclip()
{
	UpdateData();

	m_pFclip->nXRep = m_nXRep;
	m_pFclip->nYRep = m_nYRep;

	m_pFclip->nOffX = m_nOffX;
	m_pFclip->nOffY = m_nOffY;

	if(m_fAngle != m_pFclip->fAngle)
	{
		m_pFclip->fAngle = m_fAngle;
		m_pFclip->Transfer();	
	}
	
	Invalidate();
}

void CFclipDlg::OnCancel() 
{
	if(m_pFclip)
	{
		m_pFclip->nXRep = m_bknXRep;
		m_pFclip->nYRep = m_bknYRep;

		m_pFclip->nOffX = m_bknOffX;
		m_pFclip->nOffY = m_bknOffY;

		if(m_bkfAngle != m_pFclip->fAngle)
		{
			m_pFclip->fAngle = m_bkfAngle;
			m_pFclip->Transfer();
		}
	}
	CDialog::OnCancel();
}

void CFclipDlg::OnOK() 
{
	UpdateData();

	if(m_pFclip)
	{
		m_pFclip->nPosX = m_nPosX;
		m_pFclip->nPosY = m_nPosY;
		m_pFclip->bLock = m_bLock;
	}
	CDialog::OnOK();
}
